'use strict';
//获取选项A = 65
let eng = 65;
//获取题目
let questionDates = [];
questionDates.push("{\n" +
    "    \"question\":\"《草原就是我的家》是哪个民族的民歌？\",\n" +
    "    \"type\":\"单选题\",\n" +
    "    \"optionnum\":3,\n" +
    "    \"option\":\"蒙古族|傣族|侗族\",\n" +
    "    \"answernum\":1,\n" +
    "    \"answer\":\"蒙古族\",\n" +
    "    \"weight\":\"2\"\n" +
    "}");
questionDates.push("{\n" +
    "    \"question\":\"《草原就是我的家》是[]的[]歌曲？\",\n" +
    "    \"type\":\"单空题\",\n" +
    "    \"spacenum\":2,\n" +
    "    \"answernum\":2,\n" +
    "    \"answer\":\"蒙古族|民族\",\n" +
    "    \"weight\":\"2\"\n" +
    "}");

function parseQuestionDates(i) {
//把所有的JSON文件转换成JS文件，并且生成标签插入到页面里面去
        let questionObj = JSON.parse(questionDates[i]);
    //判断是选择题还是填空题
    if (questionObj.option === undefined) {
        //如果是填空题
        //加入题目
        let question = questionObj.question.split("[]");
        newSpan('examArea' + i,questionObj.question);
        newH('' + questionObj.question,question[0] );
        for (let j = 1; j < question.length; j++) {
            newTextBox('' + questionObj.question);
            newH('' + questionObj.question,question[j] );
        }
        //加入答案
        let answer = questionObj.answer.split("|");
        newSpan('examArea' + i,questionObj.answer);
            newH('' + questionObj.answer, '答案是');
        for (let j = 0; j < answer.length; j++) {
            newH('' + questionObj.answer, answer[j]);
        }
    } else {
        //如果不是填空题
        //加入题目
        let questionObj = JSON.parse(questionDates[i]);
        newH('examArea' + i, questionObj.question);
        //加入选项
        let myquestion = questionObj.option.split("|");
        newSpan('examArea' + i,questionObj.option);
        for (let j = 0; j < myquestion.length; j++) {
            newButton('' + questionObj.option, String.fromCharCode(65+j)+myquestion[j]);
        }
        //加入选项
        newSpan('examArea' + i,questionObj.answer);
        newH('' + questionObj.answer, '答案是');
        newH('' + questionObj.answer, questionObj.answer);
    }
}

// 数组里面有多少个数组输出多少个题目
for (let i = 0; i < questionDates.length; i++) {
    //调用parseQuestionDates()；
    let oDivNode1 = document.getElementById("num1");
    let newDiv1 = document.createElement('div');
    newDiv1.id = 'examArea' + i;
    newDiv1.innerText = '题目' + (i + 1);
    oDivNode1.appendChild(newDiv1);
    parseQuestionDates(i);
}

//生成h4标签的方法,参数分别是父类标签和文本内容
function newH(x, text) {
    let oDivNode1 = document.getElementById("" + x);
    let newDiv1 = document.createElement('h4');
    newDiv1.innerText = '' + text;
    oDivNode1.appendChild(newDiv1);
}

//生成Button标签的方法，参数分别是父类标签和按钮上的文字
function newButton(x, text) {
    let oDivNode1 = document.getElementById("" + x);
    let newDiv1 = document.createElement('input');
    newDiv1.type = 'button';
    newDiv1.value = '' + text;
    oDivNode1.appendChild(newDiv1);
}

//生成Span标签的方法，参数分别是父类标签和id值
function newSpan(x, text) {
    let oDivNode1 = document.getElementById("" + x);
    let newDiv1 = document.createElement('span');
    newDiv1.id = '' + text;
    oDivNode1.appendChild(newDiv1);
}

//生成文本框标签，参数是父类标签
function newTextBox(x) {
    let oDivNode1 = document.getElementById("" + x);
    let newDiv1 = document.createElement('input');
    newDiv1.type = 'text';
    newDiv1.className='textBox';
    oDivNode1.appendChild(newDiv1);
}